# encoding=utf-8
from LBT.BaseParent.BaseLbt import lbtlogin
from LBT.Resource.StudentInfo.StudentInfoAtt import StudentInfoAtt
from LBT.GeneralOperation.StudentOperation import StudentOperation

class GraduationStudent(lbtlogin, StudentInfoAtt):
    '''毕业幼儿模块
    1、查询页面，和数据库核对，然后和详情页面核对，然后下载和下载文件核对
    2、重新入校，然后去幼儿档案模块核对
    '''

    @lbtlogin.loginFun
    def test_checkGraduastudent(self):
        """
        查询页面，和数据库核对，然后和详情页面核对，然后下载和下载文件核对
        :return:
        """
        self.docstr = '查询页面，和数据库核对，然后和详情页面核对，然后下载和下载文件核对'
        dr = self.driver
        # 进入到毕业幼儿模块，先查询一个已经存在的学生
        self.moduleOp.openMoudleAndIn(self.studentinfo_navigation_class, self.studentinfo_Graduate_url,
                                      self.student_topnavigation_class)
        stuopr = StudentOperation(dr)
        #查询第一个学生，然后和数据库校验
        firststudent = dr.find_elements_by_xpath(self.studentinfo_students_xpath)[0]
        infodict = stuopr.getstudentinfo(firststudent)
        #然后再和详情页面校验
        dr.find_elements_by_css_selector(self.studentinfo_gradstudetail_css)[0].click()
        #firststudent.find_elements_by_tag_name("button")[0].click()
        self.webdriverwait(dr, dr.find_element_by_css_selector(self.studentinfo_childdetailinfo_css))
        childjichu = dr.find_element_by_css_selector(self.studentinfo_childdetailinfo_css)
        details = str(childjichu.text).split("\n")
        detaildict = {}
        detaildict['name'] = str(details[2])
        detaildict['class'] = str(details[4])
        detaildict['sex'] = str(details[6])
        detaildict['phone'] = str(details[10])
        self.assertTrue(infodict == detaildict)
        infodict["class"] = str(infodict["class"]).split("(")[0]
        sql = "select u.state from users u inner join student st on st.studentid = u.usersid inner join eduunit edu on edu.eduunitid = st.eduunitid where u.phone = '" + \
              infodict["phone"] + "' and u.usertype =2 and u.name = '" + infodict[
                  "name"] + "' and edu.level=5 and edu.eduunitname = '" + infodict["class"] + "'"
        self.assertTrue(int(self.selectlbt(sql)) == 3)
        #然后再和导出的文件校验
        downbtn=dr.find_element_by_id(self.studentinfo_gradstudownbtn_id)
        downbtn.location_once_scrolled_into_view
        self.deleteAllFiles(self.defaultdownpath)
        downbtn.click()
        contentlist=self.readExcle(self.defaultdownpath)
        self.assertEqual(str(contentlist[0][1]),detaildict['name'])
        self.assertEqual(str(contentlist[0][3]), detaildict['class'])
        self.assertEqual(str(contentlist[0][5]), detaildict['sex'])

    @lbtlogin.loginFun
    def test_reGraduastudent(self):
        """
        重新入校，然后去幼儿档案模块核对
        :return:
        """
        self.docstr = '重新入校，然后去幼儿档案模块核对'
        dr = self.driver
        # 进入到毕业幼儿模块，先查询一个学生
        self.moduleOp.openMoudleAndIn(self.studentinfo_navigation_class, self.studentinfo_Graduate_url,
                                      self.student_topnavigation_class)
        stuopr = StudentOperation(dr)
        # 查询第一个学生，然后和数据库校验
        firststudent = dr.find_elements_by_xpath(self.studentinfo_students_xpath)[0]
        infodict = stuopr.getstudentinfo(firststudent)
        #展开详情页
        firststudent.find_elements_by_tag_name("button")[0].click()
        self.webdriverwait(dr,dr.find_element_by_css_selector(self.studentinfo_gradStuEnterPage_css))
        #找到第一个班级，然后入校
        #classname=dr.find_elements_by_xpath(self.chosen_namelist_xpath)[1].text
        stuopr = StudentOperation(dr)
        classname=stuopr.selectWithInput(dr,1,1,1)
        dr.find_element_by_class_name(self.studentinfo_gradstuOk_class).click()
        #去幼儿档案模块查看校验
        dr.switch_to.default_content()
        # 关闭所有之前的模块，重新打开
        self.moduleOp.close_modules()
        self.moduleOp.openMoudleAndIn(self.studentinfo_navigation_class, self.studentinfo_studentinfo_url,
                                      self.student_topnavigation_class)
        # 查询刚才的学生
        stuopr.selectWithInput(dr, 0, 0, classname)
        dr.find_element_by_id(self.studentinfo_name_id).send_keys(infodict["name"])
        dr.find_element_by_id(self.studentinfo_phone_id).send_keys(infodict["phone"])
        dr.find_element_by_xpath(self.studentinfo_selectbutton_xpath).click()
        studentinfos = dr.find_elements_by_xpath(self.studentinfo_students_xpath)
        self.assertTrue(studentinfos.__len__() == 1)
        firststudentnew = studentinfos[0]
        infodictnew = stuopr.getstudentinfo(firststudentnew)
        self.assertEqual(infodictnew["name"], infodict["name"])
        self.assertTrue(str(infodictnew["class"]).__contains__(classname))
        self.assertTrue(str(infodictnew["sex"]) == str(infodict["sex"]))
        self.assertTrue(str(infodictnew["phone"]) == str(infodict["phone"]))